"use strict"

const Service = require('egg').Service

class GoodsService extends Service {

    /**
     * @param {商品详情} param 
     */
    async details(param) {
        console.log(param.id)
        const { app, ctx } = this
        const id = param.id
        try{
            const result = await app.model.Goods.findOne({
                where: {id},
                include: [
                    {
                        model: app.model.GoodsImgs,
                        as: 'imgs',
                        attributes: ['img_path'],
                    },
                    {
                        model: app.model.AttributeKey,
                        as: 'attributeList',
                        attributes: ['name'],
                        include: [
                            {
                                model: app.model.AttributeValue,
                                as: 'children',
                                attributes: ['value'],
                            },
                        ]
                    },
                    {
                        model: app.model.GoodsSku,
                        as: 'goods_specs',
                        attributes: ['attribute_specs', 'sales_price', 'price', 'inventory'],
                    },
                ]
            })
            return ctx.app.returnHint(result, '查询成功').definedSucceed
        }catch(err) {
            console.log(err)
            return ctx.app.returnHint(err, '查询失败').definedError
        }
    }
}

module.exports = GoodsService